package cn.sxt.持久化topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Topic生产者
 *
 * @author Mxb
 * @version 1.0
 * @date 2020/6/10 15:01
 */
public class TestTopicProducer {
    //地址在mq conf activemq.xml里面找
    private static final String BROKER_URL = "tcp://39.99.166.61:61616";
    private static final String TOPIC_NAME = "mq-topic-test";

    public static void main(String[] args) throws JMSException {
        ConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);

        Connection connection = factory.createConnection();


        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic(TOPIC_NAME);
        MessageProducer producer = session.createProducer(topic);

        // 主题设置持久化
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        // 开启持久化后 开启
        connection.start();


        for (int i = 0; i < 10; i++) {
            TextMessage textMessage = session.createTextMessage("topic-message" + i);

            //textMessage.setJMSDeliveryMode(DeliveryMode.PERSISTENT);

            producer.send(textMessage);


            System.out.println("消息发送成功。。。" + i);
        }
        producer.close();
        session.close();
        connection.close();
    }
}
